package com.gobo.jinghuimall.product.service.impl;

import com.gobo.jinghuimall.product.dao.BrandDao;
import com.gobo.jinghuimall.product.dao.CategoryDao;
import com.gobo.jinghuimall.product.entity.BrandEntity;
import com.gobo.jinghuimall.product.service.BrandService;
import com.gobo.jinghuimall.product.vo.BrandCategoryRelationVo;
import com.gobo.jinghuimall.product.vo.CategoryBrandRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gobo.common.utils.PageUtils;
import com.gobo.common.utils.Query;

import com.gobo.jinghuimall.product.dao.CategoryBrandRelationDao;
import com.gobo.jinghuimall.product.entity.CategoryBrandRelationEntity;
import com.gobo.jinghuimall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationVo> queryBrandsList(Map<String, Object> params) {
        String catId = (String) params.get("catId");
        QueryWrapper<CategoryBrandRelationEntity> wa = new QueryWrapper<>();
        wa.eq("catelog_id", catId);
        List<CategoryBrandRelationEntity> list = this.list(wa);
        List<CategoryBrandRelationVo> collect = list.stream().map(obj -> {
            CategoryBrandRelationVo relationVo = new CategoryBrandRelationVo();
            BeanUtils.copyProperties(obj, relationVo);
            return relationVo;
        }).collect(Collectors.toList());
        return collect;
    }

    //queryCategoryListBybrandId
    @Override
    public List<CategoryBrandRelationEntity> queryCategoryListBybrandId(Map<String, Object> params) {
        String brandId = (String) params.get("brandId");
        QueryWrapper<CategoryBrandRelationEntity> wa = new QueryWrapper<>();
        wa.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> list = this.list(wa);

        return list;
    }

    /**
     * 保存分类与品牌关联关系
     * Boolean saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelationEntity)
     *
     * @param categoryBrandRelationEntity
     * @return
     */
    @Override
    @Transactional
    public Boolean saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelationEntity) {
        Long brandId = categoryBrandRelationEntity.getBrandId();
        Long catelogId = categoryBrandRelationEntity.getCatelogId();
        if (brandId == 0L || catelogId == 0L) {
            return false;
        }
        categoryBrandRelationEntity.setCatelogName(categoryDao.selectById(catelogId).getName());
        categoryBrandRelationEntity.setBrandName(brandDao.selectById(brandId).getName());
        boolean save = this.save(categoryBrandRelationEntity);
        return save;
    }

}